package com.hnkywl.sts.action.tjbb;

import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.tjbb.KhwlDao;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.service.tjbb.KhwlService;
import com.hnkywl.sts.util.POIUtil;
import com.hnkywl.sts.util.StringFormat;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.Region;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: SDE09
 * Date: 13-9-28
 * Time: 下午5:06
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/tjbb/khwl")
public class KhwlAction extends SimpleActionSupport {
    private KhwlService khwlService;
    private KhwlDao khwlDao;
    private GsjgDao gsjgDao;

    private Integer gsid;
    private Integer deptid;
    private Date enddt;
    private String custname;
    private int page;
    private int rows;
    private Integer tc_flag;
    private Date begindt;
    private Integer type;
    private Gsjg kh;


    @Action("khwl")
    public String khwl() {
        if (enddt == null) {
            enddt = new Date();
        }
        return SUCCESS;
    }

    @Action("khwlList")
    public void khwlList() {
        Calendar calendar = Calendar.getInstance();
        if (enddt == null) {
            //得到月末
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            enddt = calendar.getTime();
        }
        if (tc_flag == null) {
            tc_flag = 1;
        }

        String json = khwlService.KhwlList(gsid, deptid, enddt, custname, page, rows, tc_flag);
        renderJson(json);
    }

    //客户往来对账跳转
    @Action("khwldz")
    public String khwlDz() {
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(enddt);
        startCal.set(Calendar.DAY_OF_MONTH, 1);
        begindt = startCal.getTime();
        try {
            custname = URLDecoder.decode(custname, "UTF-8");
            kh = gsjgDao.findGsjgFromQc(custname);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return SUCCESS;
    }

    //客户往来对账调用集合
    @Action("khwldzlist")
    public void khwlDzlist() {
        renderJson(khwlService.KhwlDz(gsid, deptid, begindt, enddt, custname, type, page, rows));
    }

    //销售未结算金额
    @Action("xswjsje")
    public String xswjsje() {
        Calendar calendar = Calendar.getInstance();
        if (enddt == null) {
            //得到月末
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            enddt = calendar.getTime();
            // enddt = new Date();
        }
        try {
            custname = URLDecoder.decode(custname, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    @Action("xswjsjelist")
    public void xswjsjelist() {
        renderJson(khwlService.xswjsje(gsid, enddt, custname));
    }

    @Action("khwl_export")
    public String khwlExport() {
        Map<String, List<Map>> khwlInfo = khwlDao.khwlListForExport(gsid, deptid, enddt, custname, 1, 9999, tc_flag);
        HSSFWorkbook excel = new HSSFWorkbook();
        POIUtil poiUtil = new POIUtil(excel);
        HSSFCellStyle cellStyleCenter = poiUtil.createContent3HSSFCellStyle(excel);
        HSSFCellStyle cellStyleLeft = poiUtil.createContent4HSSFCellStyle(excel);
        HSSFCellStyle headerCelleStyle = poiUtil.createHeaderHSSFCellStyle(excel);
        HSSFCellStyle titleStyle = poiUtil.createTitleHSSFCellStyle(excel);
        HSSFSheet sheet = excel.createSheet();
        excel.setSheetName(0, "客户往来", HSSFWorkbook.ENCODING_UTF_16);
        sheet.setDefaultColumnWidth((short) 15);
        sheet.setColumnWidth((short) 0, (short) (3 * 512));
        sheet.setColumnWidth((short) 1, (short) (12 * 512));
        sheet.setColumnWidth((short) 2, (short) (12 * 512));
        sheet.setColumnWidth((short) 3, (short) (12 * 512));
        sheet.setColumnWidth((short) 4, (short) (12 * 512));
        sheet.setColumnWidth((short) 5, (short) (12 * 512));
        sheet.setColumnWidth((short) 6, (short) (12 * 512));
        sheet.setColumnWidth((short) 7, (short) (12 * 512));
        sheet.setColumnWidth((short) 8, (short) (12 * 512));
        sheet.setColumnWidth((short) 9, (short) (12 * 512));


        HSSFRow row0 = sheet.createRow((int) 0);
        row0.setHeightInPoints(36);
        HSSFCell titleCell = row0.createCell((short) 0);
        titleCell.setCellStyle(titleStyle);
        titleCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        titleCell.setCellValue("客户往来导出表");
        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 9));

        HSSFRow row1 = sheet.createRow((int) 1);
        row1.setHeightInPoints(21);
        HSSFCell xh = row1.createCell((short) 0);
        xh.setCellStyle(headerCelleStyle);
        xh.setEncoding(HSSFCell.ENCODING_UTF_16);
        xh.setCellValue("序号");
        HSSFCell gsmc = row1.createCell((short) 1);
        gsmc.setCellStyle(headerCelleStyle);
        gsmc.setEncoding(HSSFCell.ENCODING_UTF_16);
        gsmc.setCellValue("公司名称");
        HSSFCell gysmc = row1.createCell((short) 2);
        gysmc.setCellStyle(headerCelleStyle);
        gysmc.setEncoding(HSSFCell.ENCODING_UTF_16);
        gysmc.setCellValue("客户名称");
        HSSFCell ywyufk = row1.createCell((short) 3);
        ywyufk.setCellStyle(headerCelleStyle);
        ywyufk.setEncoding(HSSFCell.ENCODING_UTF_16);
        ywyufk.setCellValue("业务预收款");
        HSSFCell ywyingfk = row1.createCell((short) 4);
        ywyingfk.setCellStyle(headerCelleStyle);
        ywyingfk.setEncoding(HSSFCell.ENCODING_UTF_16);
        ywyingfk.setCellValue("业务应收款");
        HSSFCell xswjssl = row1.createCell((short) 5);
        xswjssl.setCellStyle(headerCelleStyle);
        xswjssl.setEncoding(HSSFCell.ENCODING_UTF_16);
        xswjssl.setCellValue("销售未结算数量");
        HSSFCell xswjsje = row1.createCell((short) 6);
        xswjsje.setCellStyle(headerCelleStyle);
        xswjsje.setEncoding(HSSFCell.ENCODING_UTF_16);
        xswjsje.setCellValue("销售未结算金额");
        HSSFCell cwyusk = row1.createCell((short) 7);
        cwyusk.setCellStyle(headerCelleStyle);
        cwyusk.setEncoding(HSSFCell.ENCODING_UTF_16);
        cwyusk.setCellValue("财务预收款");
        HSSFCell cwyingsk = row1.createCell((short) 8);
        cwyingsk.setCellStyle(headerCelleStyle);
        cwyingsk.setEncoding(HSSFCell.ENCODING_UTF_16);
        cwyingsk.setCellValue("财务应收款");


        int i = 2;
        for (; i < khwlInfo.get("rows").size() + 2; i++) {
            Map<String, Object> khwlRow = khwlInfo.get("rows").get(i - 2);
            HSSFRow row = sheet.createRow(i);
            row.setHeightInPoints(19);
            row = sheet.createRow(i);
            xh = row.createCell((short) 0);
            xh.setCellStyle(cellStyleCenter);
            xh.setEncoding(HSSFCell.ENCODING_UTF_16);
            xh.setCellValue(Integer.toString(i - 1));

            gsmc = row.createCell((short) 1);
            gsmc.setCellStyle(cellStyleCenter);
            gsmc.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != khwlRow.get("gs_name")) {
                gsmc.setCellValue(khwlRow.get("gs_name").toString());
            }

            gysmc = row.createCell((short) 2);
            gysmc.setCellStyle(cellStyleCenter);
            gysmc.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != khwlRow.get("cust_name")) {
                gysmc.setCellValue(khwlRow.get("cust_name").toString());
            }


            ywyufk = row.createCell((short) 3);
            ywyufk.setCellStyle(cellStyleCenter);
            ywyufk.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != khwlRow.get("yw_yusk")) {
                ywyufk.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                ywyufk.setCellValue(((BigDecimal) khwlRow.get("yw_yusk")).doubleValue());
            }


            ywyingfk = row.createCell((short) 4);
            ywyingfk.setCellStyle(cellStyleCenter);
            ywyingfk.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != khwlRow.get("yw_yingsk")) {
                ywyingfk.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                ywyingfk.setCellValue(((BigDecimal) khwlRow.get("yw_yingsk")).doubleValue());
            }


            xswjssl = row.createCell((short) 5);
            xswjssl.setCellStyle(cellStyleCenter);
            xswjssl.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != khwlRow.get("xswjs_sl")) {
                xswjssl.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                xswjssl.setCellValue(((BigDecimal) khwlRow.get("xswjs_sl")).doubleValue());
            }

            xswjsje = row.createCell((short) 6);
            xswjsje.setCellStyle(cellStyleCenter);
            xswjsje.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != khwlRow.get("xswjs_je")) {
                xswjsje.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                xswjsje.setCellValue(((BigDecimal) khwlRow.get("xswjs_je")).doubleValue());
            }

            cwyusk = row.createCell((short) 7);
            cwyusk.setCellStyle(cellStyleCenter);
            cwyusk.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != khwlRow.get("cw_yufk")) {
                cwyusk.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                cwyusk.setCellValue(((BigDecimal) khwlRow.get("cw_yufk")).doubleValue());
            }

            cwyingsk = row.createCell((short) 8);
            cwyingsk.setCellStyle(cellStyleCenter);
            cwyingsk.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != khwlRow.get("cw_yingsk")) {
                cwyingsk.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                cwyingsk.setCellValue(((BigDecimal) khwlRow.get("cw_yingsk")).doubleValue());
            }
        }

        HSSFRow rowFooter = sheet.createRow(i);
        rowFooter.setHeightInPoints(21);
        HSSFCell footerCell = rowFooter.createCell((short) 0);
        footerCell.setCellStyle(titleStyle);
        footerCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        Map<String, Object> khwlFooter = khwlInfo.get("footer").get(0);
        String footer = StringFormat.format("合计：业务预收款：{0}，业务应收款：{1}，销售未结算数量{2}，销售未结算金额：{3}，财务预收款：{4}，财务应收款{5}", khwlFooter.get("yw_yusk").toString()
                , khwlFooter.get("yw_yingsk").toString(), khwlFooter.get("xswjs_sl").toString(), khwlFooter.get("xswjs_je").toString(), khwlFooter.get("cw_yusk").toString(), khwlFooter.get("cw_yingsk").toString());
        footerCell.setCellValue(footer);
        sheet.addMergedRegion(new Region(i, (short) 0, i, (short) 15));

        try {
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
            String filename = StringFormat.format("客户往来_{0}", dateformat.format(new Date()));
            response.setContentType("application/x-download");
            String filedisplay = new String((filename + ".xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            excel.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }
    //==============getter====setter=================


    public Integer getGsid() {
        return gsid;
    }

    public void setGsid(Integer gsid) {
        this.gsid = gsid;
    }

    public Integer getDeptid() {
        return deptid;
    }

    public void setDeptid(Integer deptid) {
        this.deptid = deptid;
    }

    public Date getEnddt() {
        return enddt;
    }

    public void setEnddt(Date enddt) {
        this.enddt = enddt;
    }

    public String getCustname() {
        return custname;
    }

    public void setCustname(String custname) {
        this.custname = custname;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Integer getTc_flag() {
        return tc_flag;
    }

    public void setTc_flag(Integer tc_flag) {
        this.tc_flag = tc_flag;
    }

    public Date getBegindt() {
        return begindt;
    }

    public void setBegindt(Date begindt) {
        this.begindt = begindt;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Gsjg getKh() {
        return kh;
    }

    public void setKh(Gsjg kh) {
        this.kh = kh;
    }

    @Autowired
    public void setKhwlService(KhwlService khwlService) {
        this.khwlService = khwlService;
    }

    @Autowired
    public void setKhwlDao(KhwlDao khwlDao) {
        this.khwlDao = khwlDao;
    }

    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }
}
